package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.exam.ExamQueryRequest;
import com.gr.smartedubackend.model.entity.Exam;
import com.gr.smartedubackend.service.ExamService;
import com.gr.smartedubackend.mapper.ExamMapper;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.stereotype.Service;

/**
 * @author 尘轩
 * @description 针对表【exam(考试信息)】的数据库操作Service实现
 * @createDate 2025-07-02 15:01:25
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam>
        implements ExamService {

    @Override
    public QueryWrapper<Exam> getQueryWrapper(ExamQueryRequest examQueryRequest) {

        ThrowUtils.throwIf(examQueryRequest == null, ErrorCode.PARAMS_ERROR);

        String examName = examQueryRequest.getExamName();
        String courseName = examQueryRequest.getCourseName();
        String className = examQueryRequest.getClassName();
        String examType = examQueryRequest.getExamType();
        String examPlace = examQueryRequest.getExamPlace();
        String sortField = examQueryRequest.getSortField();
        String sortOrder = examQueryRequest.getSortOrder();

        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(examName), "examName", examName);
        queryWrapper.like(StrUtil.isNotEmpty(courseName), "courseName", courseName);
        queryWrapper.like(StrUtil.isNotEmpty(className), "className", className);
        queryWrapper.like(StrUtil.isNotEmpty(examType), "examType", examType);
        queryWrapper.like(StrUtil.isNotEmpty(examPlace), "examPlace", examPlace);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }
}




